<?php

namespace App\Http\Controllers;

use App\Exceptions\InvalidParameterException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Http\Exception\HttpResponseException;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected function throwValidationException(Request $request, $validator)
    {
    	if(request()->ajax() || request()->wantsJson()){
			throw new InvalidParameterException('exceptions.invalidParameter', $validator->errors()->first());
    	}
    	throw new HttpResponseException($this->buildFailedValidationResponse(
    	    $request, $this->formatValidationErrors($validator)
    	));
    }
}
